package dhm.com.hpx.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.h6ah4i.android.widget.advrecyclerview.draggable.ItemDraggableRange;

import java.util.List;

import dhm.com.hpx.R;
import dhm.com.hpx.adapter.holder.MenuHeaderRecyclerGridHolder;
import dhm.com.hpx.entity.MenuItem;
import dhm.com.hpx.recyclerview.BaseDraggableRecyclerAdapter;


/**
 * 描述:编辑页面的头部的子元素适配器
 * <p>
 * 作者:陈俊森
 * 创建时间:2017年11月03日 17:07
 * 邮箱:chenjunsen@outlook.com
 *
 * @version 1.0
 */
public class MenuHeaderRecyclerGridAdapter extends BaseDraggableRecyclerAdapter<MenuHeaderRecyclerGridHolder, MenuItem> {
    private boolean showEditIcon;
    private int maxSlots;
    private OnDeleteClickListener mOnDeleteClickListener;

    public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
        mOnDeleteClickListener = onDeleteClickListener;
    }

    /**
     * 可以拖拽的RecyclerView适配器
     *
     * @param recyclerItems 数据列表
     * @param recyclerView  与改适配器匹配的RecyclerView
     */
    public MenuHeaderRecyclerGridAdapter(List<MenuItem> recyclerItems, RecyclerView recyclerView, boolean showEditIcon) {
        super(recyclerItems, recyclerView);
        this.showEditIcon = showEditIcon;
    }

    @Override
    public MenuHeaderRecyclerGridHolder createRecyclerViewHolder(ViewGroup parent, int viewType) {
        int layout = R.layout.item_menu_grid_func;
        if (viewType == MenuItem.VIEW_TYPE_DASH) {
            layout = R.layout.item_menu_grid_dash;
        }
        return new MenuHeaderRecyclerGridHolder(LayoutInflater.from(parent.getContext()).inflate(layout, parent, false));
    }

    public int getMaxSlots() {
        return maxSlots;
    }

    public void setMaxSlots(int maxSlots) {
        this.maxSlots = maxSlots;
    }

    @Override
    public void bindViewHolder(final MenuHeaderRecyclerGridHolder holder, MenuItem item) {
        final MenuItem menuItem = item;
        if (item.getViewType() != MenuItem.VIEW_TYPE_DASH) {
            holder.tv_delete.setVisibility(showEditIcon ? View.VISIBLE : View.GONE);
            holder.tv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnDeleteClickListener != null) {
                        mOnDeleteClickListener.onDeleteClick(v, menuItem, holder.getAdapterPosition());
                    }
                }
            });
            holder.tv_name.setText(item.getName());
        }
    }

    @Override
    public int getItemCount() {
        return mRecyclerItems == null ? 0 : mRecyclerItems.size();
    }

    @Override
    public ItemDraggableRange onGetItemDraggableRange(MenuHeaderRecyclerGridHolder holder, int position) {
        MenuItem lastItem=mRecyclerItems.get(getItemCount()-1);
        if(lastItem.getViewType() == MenuItem.VIEW_TYPE_DASH){
            return new ItemDraggableRange(0, getItemCount() - 2);
        }else{
            return new ItemDraggableRange(0, getItemCount() - 1);
        }
    }

    /**
     * 描述:删除脚标单击监听器
     * <p>
     * <br>作者: 陈俊森
     * <br>创建时间: 2017/11/6 0006 14:54
     * <br>邮箱: chenjunsen@outlook.com
     *
     * @version 1.0
     */
    public interface OnDeleteClickListener {
        void onDeleteClick(View v, MenuItem item, int position);
    }
}
